<?PHP
namespace app\system\controller;
use \think\Controller;
use think\Db;

class SuperAdmin extends Base
{
    public function _initialize() {
        parent::_initialize ();
        if(!empty($this->data['p_str']) && !empty($this->data['p_time'])){
            $crypt = new \Crypt();
            $encrypt_auth = $crypt->decrypt($this->data['p_str'], lang_config('auth_key',1));
            $ping_str = $this->data['domain'].'@'.$this->data['p_time'];
            if($encrypt_auth !== $ping_str){
                print_r(json_encode(api_return(-11709)));exit;
            }else{
                $this->data['no_token']=1;
            }
        }
    }
    /**
     * @return mixed
     * 管理员列表
     */
    public function lists(){
        $where = 1;
        $intkeys = ['status','site_type'];
        $strkeys = [];
        $likekeys = [];
        $inkeys = [];
        $where .=  \app\common\model\Base::buildSearch($intkeys, $strkeys, [], $likekeys,$inkeys);
        if(!empty($this->data['add_domain'])){
            $where .= " and domain like '%{$this->data['add_domain']}%'";
        }
        $param = \app\common\model\Base::list_param('addtime','desc');
        $list = model('SuperAdmin')->admin_list($where,$param);
        if(false !== $list){
            return api_return(10010,$list);
        }
        return api_return(-10002);
    }

    /**
     * @return mixed
     * 管理员编辑或添加
     */
    public function updata(){
        if(empty($this->data['add_domain'])) return api_return(-11402);

        $web_info = self::check_domain($this->data['add_domain']);
        cache('getwebid_'.$this->data['add_domain'],null,get_cache_path());
        if(false === $web_info ) return api_return(-22000);
        $this->data = array_merge($this->data,$web_info);

        $check = $this->validate($this->data,'SuperAdmin.updata');
        if(true !== $check){
            return api_return($check);
        }
        if(empty($this->data['id'])){
            unset($this->data['id']);
        }
        $ok = model('SuperAdmin')->updata_admin($this->data);
        if(false !== $ok){
            return api_return(10603);
        }
        return api_return(-10666);
    }
    /**
     * @return mixed
     * 管理员删除
     */
    public function del(){
        $check = $this->validate($this->data,'SuperAdmin.del');
        if(true !== $check){
            return api_return($check);
        }

        $ok = model('SuperAdmin')->del_admin($this->data);
        if(false !== $ok){
            return api_return(10603);
        }
        return api_return(-10666);
    }
    public function detail(){
        $check = $this->validate($this->data,'SuperAdmin.del');
        if(true !== $check){
            return api_return($check);
        }
        $list = model('SuperAdmin')->detail(['id'=>$this->data['id']]);
        if(false !== $list){
            return api_return(10010,$list);
        }
        return api_return(-10002);
    }
    /**
     * @return mixed
     * 域名检测
     */
    private function check_domain($domain){
        $mall_domain = \app\common\model\Base::get_domain_set('mall_domain',1);
        $domain = \app\common\model\Base::check_domain($mall_domain.'/api/domain/get_web',['domain'=>$domain]);
        if(empty($domain['web_id'])){
            return false;
        }
        return $domain;
    }
}